<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';


class dao_news {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT nc.cate_news_name AS news_cat_name, n.* FROM tbl_news n LEFT JOIN tbl_news_category nc ON n.cat_news_id = nc.cat_news_id";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($news_id, $news_title, $news_content, $cat_news_id, $image) {
        $db = new connection();
        $con = $db->open_connect();

        if ($news_id == 0) {
            $query = "INSERT INTO tbl_news(news_title, news_content, cat_news_id, news_added_date, news_img_url) VALUES (
                '" . $news_title . "','" . $news_content . "'," . $cat_news_id . ",'" . lib_date::get_now() . "','" . $image . "')";
        } else {
            $query = "UPDATE tbl_news SET "
                    . "news_title = '" . $news_title . "',"
                    . "news_content = '" . $news_content . "',"
                    . "cat_news_id = " . $cat_news_id . ","
                    . "news_added_date = '" . lib_date::get_now() . "',"
                    . "news_img_url = '" . $image . "' "
                    . "WHERE news_id = " . $news_id;
        }
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_news WHERE news_id = " . $id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_news WHERE news_id = " . $id;
        /** @noinspection PhpParamsInspection */
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function count_by_category_id($category_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_news WHERE cat_news_id = " . $category_id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return count($list);
    }
    
    public function get_all_by_category_id($category_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_news WHERE cat_news_id = " . $category_id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
    public function get_all_latest($num_news) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_news ORDER BY news_added_date DESC LIMIT 0, " . $num_news;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
}
